package zj.it.bhne.huanxin.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import zj.it.bhne.huanxin.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText name;
    private EditText pass;
    private Context context;
    private Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        initView();
        //HuanApplication huanApplication = new HuanApplication();
        //String pid = huanApplication.getAppName(1);
        View views = new View(context);
        ViewGroup.LayoutParams params = new ViewGroup
                .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 60);
        views.setLayoutParams(params);
        views.setBackgroundColor(Color.rgb(18, 150, 219));

        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        decorView.addView(views);
    }

    private void initView() {
        name = findViewById(R.id.name);
        pass = findViewById(R.id.pass);
        Button login = findViewById(R.id.login);

        login.setOnClickListener(this);
        register = (Button) findViewById(R.id.register);
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                if ("".equals(name.getText().toString().trim()) || "".equals(pass.getText().toString().trim())) {
                    Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                } else {
                    login(name.getText().toString().trim(), pass.getText().toString().trim());
                }
                break;
            case R.id.register:
                startActivity(new Intent(context,RegisterActivity.class));
                break;
        }
    }


    /**
     *
     * */
    private void login(final String userName, String password) {
        Log.e("TAG", "login: " + userName + "    " + password);
        EMClient.getInstance().login(userName, password, new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                Intent intent = new Intent(context, HomeActivity.class);
                intent.putExtra("login", 1);
                startActivity(intent);
                Log.e("zhujiang", "登录聊天服务器成功！");
                SharedPreferences.Editor editor = getSharedPreferences("ss",MODE_PRIVATE).edit();
                editor.putString("name",userName);
                editor.apply();
                finish();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.e("zhujiang", "登录聊天服务器失败！"+message);
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}
